<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Project: Vietsam
 * Package Model
 * Class: Customer_model
 * Author: Khoi Pham		email: pmkhoi@gmail.com
 * Created on Dec 18, 2008
 * Updated on Dec 30, 2008
 *
 * */
class Customer_model extends MY_Model {

    var $active_key=null;
    /**
     * @var unknown_type
     */
    var $id;
    /**
     * @var unknown_type
     */
    var $lastname;
    /**
     * @var unknown_type
     */
    var $firstname;
    /**
     * @var unknown_type
     */
    var $email;
    /**
     * @var unknown_type
     */
    var $id_profile;
    /**
     * @var unknown_type
     */
    var $active_email;
    /**
     * @var unknown_type
     */
    var $password;
    /**
     * @var unknown_type
     */
    var $last_password_gen;
    /**
     * @var unknown_type
     */
    var $last_login;
    /**
     * @var unknown_type
     */
    var $active;
    /*
     * @access	public
     */
    /*Add by Hung.Nguyen*/
	var $id_user=null;
	var $b_firstname=null;
	var $b_lastname=null;
	var $b_company=null;
	var $b_address=null;
	var $b_address_2=null;
	var $b_city=null;
	var $b_state=null;
	var $b_country=null;
	var $b_zipcode=null;
	var $b_phone=null;
	var $b_fax=null;
	var $s_firstname=null;
	var $s_lastname=null;
	var $s_company=null;
	var $s_address=null;
	var $s_address_2=null;
	var $s_city=null;
	var $s_state=null;
	var $s_country=null;
	var $s_zipcode=null;
	var $s_phone=null;
	var $s_fax=null;
	var $multi_profile=false;
    var $user_table="gtb_register_user";
    var $user_profiles_table="gtb_register_user_profiles";

    function __construct() {
        parent::__construct();
    }

    function User_model() {
        parent::__construct();
    }

    function insert() {
        $sql = "INSERT INTO gtb_user (id_profile, lastname, firstname, email, password, active)
				VALUES(" .
                intval($this->id_profile) . "," .
                $this->db->escape($this->lastname) . ", " .
                $this->db->escape($this->firstname) . ", " .
                $this->db->escape($this->email) . ", " .
                $this->db->escape(trim($this->password)) . ", " .
                intval($this->active) .
                ")";

        $this->db->query($sql);
        if ($this->db->affected_rows())
            return $this->db->insert_id();
        return 0;
    }

    function update($user) {
    	
         $sql = "UPDATE gtb_register_user
				SET " .
                " id_profile = " . intval($user['id_profile']) . "," .
                " firstname = " . $this->db->escape($user['firstname']) . "," .
                " lastname = " . $this->db->escape($user['lastname']) . ", " .
                " email = " . $this->db->escape(trim($user['email'])). ", " .
                " active_email = " . $this->db->escape(trim($user['active_email'])) . ", " .
        		" active_key = " . $this->db->escape(trim($user['active_key'])) . ", " .
                " password = " . $this->db->escape(trim($user['password'])) . ", " .
                " active = " . intval($user['active']) .
                " WHERE id_user = " . intval($user['id_user']);
        
        $this->db->query($sql);
        return $this->db->affected_rows();
    }

    function delete($id) {
        if (!is_array($id))
            $id = array($id);
        $ids = trim(implode(',', $id));
        if ($ids == '')
            return;

        $sql = "DELETE FROM gtb_user WHERE id_user IN (" . $ids . ")";
        $this->db->query($sql);

        return $this->db->affected_rows();
    }

    function getAll($offset = 0, $limit=0) {
        $result = null;

        if ($limit)
            $sql = 'SELECT u.*, p.vi_name as profile_name
				FROM gtb_user u
				INNER JOIN gtb_profile p ON p.id_profile = u.id_profile
				LIMIT ' . intval($offset) . ', ' . intval($limit);
        else
            $sql = 'SELECT u.*, p.vi_name as profile_name
				INNER JOIN gtb_profile p ON p.id_profile = u.id_profile
				FROM gtb_user';

        $query = $this->db->query($sql);

        $result = $query->result();

        return $result;
    }

    function getAllCount() {
        $sql = 'SELECT COUNT(*) as num_record
				FROM gtb_user u
				INNER JOIN gtb_profile p ON p.id_profile = u.id_profile
				';

        $query = $this->db->query($sql);
        if ($result = $query->result()) {
            return $result[0]->num_record;
        }

        return 0;
    }

    function getByEmailPassword($email, $password) {
        $email = trim($email); // filtler?
        $password = md5(trim($password));
        $sql = "SELECT id_user, id_profile, lastname, firstname, email, active, password, active_key
				FROM $this->user_table
				WHERE email = " . $this->db->escape($email) . " AND password = " . $this->db->escape($password) . " AND active = 1";
        $query = $this->db->query($sql);
        if ($result = $query->result_array()) {
            return $result[0];
        }

        return null;
    }

    function getByEmail($email) {
        $email = trim($email);

        //$sql = "SELECT id_user, id_profile, lastname, firstname, email, active
        
        $sql = "SELECT id_user, id_profile, lastname, firstname, email, active, password, active_key 
				FROM $this->user_table  
				WHERE email = " . $this->db->escape($email) . " OR active_email = " . $this->db->escape($email);
        $query = $this->db->query($sql);
        if ($result = $query->result_array()) {
            return $result[0];
        }

        return null;
    }

    function getById($id_user) {
        $sql = "SELECT *
				FROM gtb_register_user
				WHERE id_user = " . intval($id_user);
        $query = $this->db->query($sql);
        if ($result = $query->result_array()) {
            return $result[0];
        }

        return null;
    }

    /*Add by Hung.Nguyen*/
	function create_account($data) {//create account
//		call_user_func_array(array($this, "parse_query"), func_get_args());
//		$exist = $this->check_exist_account($this->email);
//		if(!empty($exist)){
//			return -1;//exist account;
//		}
		
        $sql = "INSERT INTO $this->user_table (lastname, firstname, email, password, last_password_gen, active_key)
				VALUES(" .
                $this->db->escape($data['lastname']) . ", " .
                $this->db->escape($data['firstname']) . ", " .
                $this->db->escape($data['email']) . ", " .
                //$this->db->escape(($data['password.":".$this->salt)) . ", " .
                $this->db->escape(($data['password'])) . ", " .
                $this->db->escape($data['last_password_gen']) . ", " .
                $this->db->escape($data['active_key']) .
                ")";

        $this->db->query($sql);
        if ($this->db->affected_rows())
            return $this->db->insert_id();
        return 0;
    }
	function update_account() {//create account
		call_user_func_array(array($this, "parse_query"), func_get_args());
		$update_pass = !empty($this->password) ? ", password = ".$this->db->escape(($this->password)) : "" ;
		$update_pass_gen = !empty($this->last_password_gen) ? ", last_password_gen = ".$this->db->escape(($this->last_password_gen))  : "" ;
        $sql = "UPDATE $this->user_table SET 
                lastname = ".$this->db->escape($this->lastname) . ", 
                firstname = ".$this->db->escape($this->firstname) . ", 
                email = ".$this->db->escape($this->email) . $update_pass . $update_pass_gen .
                " WHERE id_user = " . $this->db->escape($this->id_user);
        $this->db->query($sql);
        if ($this->db->affected_rows())
            return $this->db->affected_rows();
        return 0;
    }
	function create_profile() {//create contact address
		call_user_func_array(array($this, "parse_query"), func_get_args());
		
		if($this->multi_profile == true){//allow to create multi profile
			if(!empty($this->id_profile)){
				$this->update_main_profile($this->id_user, $this->id_profile);
			}
			//check exist main profile
			if($this->check_exist_profile($this->id_user)){//exist main profile; set profile type = S
				$this->profile_type = 'S';
				$add_insert = ', profile_type';
				$add_value = ', ' .$this->db->escape($this->profile_type) ;
			}else{
				$add_insert =$add_value ='';//default main profile
			}
			$sql = "INSERT INTO $this->user_profiles_table (id_user, b_firstname, b_lastname, b_company, b_address, b_address_2, b_city, b_state, b_country, b_zipcode, b_phone, b_fax, s_firstname, s_lastname, s_company, s_address, s_address_2, s_city, s_state, s_country, s_zipcode, s_phone, s_fax " . $add_insert . ")
				VALUES(" .
                $this->db->escape($this->id_user) . ", " .
                $this->db->escape($this->b_firstname) . ", " .
                $this->db->escape($this->b_lastname) . ", " .
                $this->db->escape($this->b_company) . ", " .
                $this->db->escape($this->b_address) . ", " .
                $this->db->escape($this->b_address_2) . ", " .
                $this->db->escape($this->b_city) . ", " .
                $this->db->escape($this->b_state) . ", " .
                $this->db->escape($this->b_country) . ", " .
                $this->db->escape($this->b_zipcode) . ", " .
                $this->db->escape($this->b_phone) . ", " .
                $this->db->escape($this->b_fax) . ", " .
                $this->db->escape($this->s_firstname) . ", " .
                $this->db->escape($this->s_lastname) . ", " .
                $this->db->escape($this->s_company) . ", " .
                $this->db->escape($this->s_address) . ", " .
                $this->db->escape($this->s_address_2) . ", " .
                $this->db->escape($this->s_city) . ", " .
                $this->db->escape($this->s_state) . ", " .
                $this->db->escape($this->s_country) . ", " .
                $this->db->escape($this->s_zipcode) . ", " .
                $this->db->escape($this->s_phone) .  ", " .
                $this->db->escape($this->s_fax)  . $add_value .
                ")";
		}else{//disallow create multi profile
			
			if($this->check_exist_profile($this->id_user)){//exist main profile (type = P); set other profile type = S
				//update
				$sql = "UPDATE $this->user_profiles_table  SET 
				b_firstname = ". $this->db->escape($this->b_firstname) . ",  
				b_lastname = ". $this->db->escape($this->b_lastname) . ", 
                b_company = ". $this->db->escape($this->b_company) . ", 
                b_address = ". $this->db->escape($this->b_address) . ", 
                b_address_2 = ". $this->db->escape($this->b_address_2) . ", 
                b_city = ". $this->db->escape($this->b_city) . ", 
                b_state = ". $this->db->escape($this->b_state) . ", 
                b_country = ". $this->db->escape($this->b_country) . ", 
                b_zipcode = ". $this->db->escape($this->b_zipcode) . ", 
                b_phone = ". $this->db->escape($this->b_phone) . ", 
                b_fax = ". $this->db->escape($this->b_fax) . ", 
                s_firstname = ". $this->db->escape($this->s_firstname) . ", 
                s_lastname = ". $this->db->escape($this->s_lastname) . ", 
                s_company = ". $this->db->escape($this->s_company) . ", 
                s_address = ". $this->db->escape($this->s_address) . ", 
                s_address_2 = ". $this->db->escape($this->s_address_2) . ", 
                s_city = ". $this->db->escape($this->s_city) . ", 
                s_state = ". $this->db->escape($this->s_state) . ", 
                s_country = ". $this->db->escape($this->s_country) . ", 
                s_zipcode = ". $this->db->escape($this->s_zipcode) . ", 
                s_phone = ". $this->db->escape($this->s_phone) .  ", 
                s_fax = ". $this->db->escape($this->s_fax) ."
                WHERE id_user = " . $this->db->escape($this->id_user);
				
			}else{//not exist => insert
				$sql = "INSERT INTO $this->user_profiles_table (id_user, b_firstname, b_lastname, b_company, b_address, b_address_2, b_city, b_state, b_country, b_zipcode, b_phone, b_fax, s_firstname, s_lastname, s_company, s_address, s_address_2, s_city, s_state, s_country, s_zipcode, s_phone, s_fax)
				VALUES(" .
                $this->db->escape($this->id_user) . ", " .
                $this->db->escape($this->b_firstname) . ", " .
                $this->db->escape($this->b_lastname) . ", " .
                $this->db->escape($this->b_company) . ", " .
                $this->db->escape($this->b_address) . ", " .
                $this->db->escape($this->b_address_2) . ", " .
                $this->db->escape($this->b_city) . ", " .
                $this->db->escape($this->b_state) . ", " .
                $this->db->escape($this->b_country) . ", " .
                $this->db->escape($this->b_zipcode) . ", " .
                $this->db->escape($this->b_phone) . ", " .
                $this->db->escape($this->b_fax) . ", " .
                $this->db->escape($this->s_firstname) . ", " .
                $this->db->escape($this->s_lastname) . ", " .
                $this->db->escape($this->s_company) . ", " .
                $this->db->escape($this->s_address) . ", " .
                $this->db->escape($this->s_address_2) . ", " .
                $this->db->escape($this->s_city) . ", " .
                $this->db->escape($this->s_state) . ", " .
                $this->db->escape($this->s_country) . ", " .
                $this->db->escape($this->s_zipcode) . ", " .
                $this->db->escape($this->s_phone) .  ", " .
                $this->db->escape($this->s_fax) . 
                ")";
			}
		}
        
        $this->db->query($sql);
        if ($this->db->affected_rows())
            return $this->db->insert_id();
        return 0;
    }
    function check_exist_profile($user_id, $main =true){
    	
    	$and = ($main == true) ? " AND profile_type = 'P' " : "";
        $sql = "SELECT id_profile 
				FROM $this->user_profiles_table   
				WHERE id_user = ". intval($user_id). $and ;
        $query = $this->db->query($sql);
        if ($result = $query->result()) {
            return true;
        }

        return false;
    }
    
    function update_main_profile($user_id, $profile_id ){
    	$this->db->query("UPDATE $this->user_profiles_table  SET profile_type = 'S' WHERE id_user = " . intval($user_id) ." AND id_profile  =  " . intval($profile_id));
        $sql = "UPDATE $this->user_profiles_table  
       		  SET profile_type = 'S' 
       		  WHERE id_user = " . intval($user_id) ."
       		  AND id_profile <> " . intval($profile_id);
        $this->db->query($sql);
        if ($this->db->affected_rows())
            return $this->db->insert_id();
        return false;
    } 
    function get_profile($user_id, $profile_id){    	
    	$profile_id = intval($profile_id);
    	$sql = "SELECT * FROM $this->user_profiles_table
    			WHERE id_user = ".intval($user_id) ;
    	$query = $this->db->query($sql);
    	if ($result = $query->result_array()) {
            foreach ($result as $v){
            	if($v['id_profile'] == $profile_id){
            		return $v;
            	}elseif($v['profile_type'] == 'P'){
            		return $v;
            	}
            }
        }
    	
    }
    function get_active_key_by_email($email){
        $sql = "SELECT active_key 
				FROM $this->user_table  
				WHERE active = 0 AND  email = ". $this->db->escape($email) ;
        $query = $this->db->query($sql);
        if ($result = $query->result()) {
            return $result[0];
        }
        return null;
    }
    function update_active_key($email, $key){
    	$sql = "UPDATE $this->user_table   
       		  SET active_key = " .  $this->db->escape($key). "
       		  WHERE email = " . $this->db->escape($email)  ;
        $this->db->query($sql);
        if ($this->db->affected_rows())
            return $this->db->insert_id();
        return false;
    }
    function active_user($email){
    	$sql = "UPDATE $this->user_table   
       		  SET active = 1  
       		  WHERE email = " . $this->db->escape($email)  ;

        $this->db->query($sql);
        if ($this->db->affected_rows()){
            return $this->db->affected_rows();
        }
        return false;
    }
    function check_exist_account($email){
     	$sql = "SELECT id_user, email 
				FROM $this->user_table  
				WHERE email = ". $this->db->escape($email) ;
        $query = $this->db->query($sql);
        if ($result = $query->result()) {
            return $result[0];
        }
    }

}

// EOF
